package com.kaiwukj.android.ufamily.mvp.ui.page.order.mine;

import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.blankj.utilcode.constant.TimeConstants;
import com.blankj.utilcode.util.TimeUtils;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.kaiwukj.android.mcas.http.imageloader.glide.GlideArms;
import com.kaiwukj.android.ufamily.R;
import com.kaiwukj.android.ufamily.mvp.http.entity.result.OrderResult;
import com.qmuiteam.qmui.widget.roundwidget.QMUIRoundButton;

import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.Date;

public class MyOrderAdapter extends BaseQuickAdapter<OrderResult, BaseViewHolder> {

    public MyOrderAdapter() {
        super(R.layout.item_order_list, new ArrayList<>());
    }

    @Override
    public long getItemId(int position) {
        return getItem(position).getId();
    }

    public void deleteOrder(int orderId) {
        int delPos = getPositionById(orderId);
        if (delPos != -1) {
            removeAt(delPos);
        }
    }

    public void invalidOrder(int orderId) {
        int pos = getPositionById(orderId);
        if (pos != -1) {
            getData().get(pos).setInvalid();
            notifyItemChanged(pos);
        }
    }

    public int getPositionById(int orderId) {
        for (int i = 0; i < getData().size(); i++) {
            if (orderId == getData().get(i).getId()) {
                return i;
            }
        }
        return -1;
    }

    @Override
    protected void convert(@NotNull BaseViewHolder holder, OrderResult result) {
        ImageView ivImage = holder.getView(R.id.iv_image);
        GlideArms.with(getContext())
                .load(result.getThumbnail())
                .diskCacheStrategy(DiskCacheStrategy.ALL)
                .into(ivImage);

        holder.setText(R.id.tv_name, result.getServiceName());
        holder.setText(R.id.tv_desc, result.getServiceParameterName());
        holder.setText(R.id.tv_num_total, String.format("共%s件", result.getNum()));
        holder.setText(R.id.tv_price, String.format("¥%s", result.getActualPrice()));
        holder.setText(R.id.tv_num, String.format("x%s", result.getNum()));
        holder.setText(R.id.tv_date, result.getAppointmentTime());
        TextView tvStatus = holder.getView(R.id.tv_status);
        //holder.setText(R.id.tv_status, result.getStatusStr());

        QMUIRoundButton btn01 = holder.getView(R.id.btn_01);
        QMUIRoundButton btn02 = holder.getView(R.id.btn_02);
        QMUIRoundButton btn03 = holder.getView(R.id.btn_03);

        int pos = holder.getLayoutPosition();

        status:

        if (result.getRefundStatus() > 0) {
            if (result.getRefundStatus() == 3) {
                tvStatus.setText("退款失败");
            } else if (result.getRefundStatus() == 1) {
                tvStatus.setText("审核中");
            } else {
                tvStatus.setText("已退款");
            }
            btn01.setVisibility(View.GONE);
            btn02.setVisibility(View.VISIBLE);
            btn03.setVisibility(View.GONE);
            btn02.setText("查看详情");
            btn02.setOnClickListener(v -> {
                if (mOnItemOrderEvent != null) mOnItemOrderEvent.onOrderDetail(pos);
            });
        } else {
            tvStatus.setText(result.getStatusStr());
            if (result.getStatus() == 0) {
                long timeSpan = TimeUtils.getTimeSpan(new Date(),
                        TimeUtils.string2Date(result.getCreateTime()), TimeConstants.SEC);
                int amountTime = (int) (15 * 60 - timeSpan);
                if (amountTime < 0) {//已超出支付时间，订单自动取消
                    /*tvStatus.setText("已失效");
                    btn01.setVisibility(View.VISIBLE);
                    btn02.setVisibility(View.VISIBLE);
                    btn03.setVisibility(View.GONE);
                    btn01.setText("再次购买");
                    btn01.setText("查看详情");
                    btn01.setOnClickListener(v -> {
                        if (mOnItemOrderEvent != null) mOnItemOrderEvent.onOrderAgain(pos);
                    });
                    btn01.setOnClickListener(v -> {
                        if (mOnItemOrderEvent != null) mOnItemOrderEvent.onOrderDetail(pos);
                    });*/
                    getData().get(pos).setInvalid();
                    onBindViewHolder(holder, pos);
                    //notifyItemChanged(holder.getAdapterPosition());//设置订单状态为已取消
                } else {
                    btn01.setText("立即支付");
                    btn02.setText("取消订单");
                    btn01.setVisibility(View.VISIBLE);
                    btn02.setVisibility(View.VISIBLE);
                    btn03.setVisibility(View.GONE);
                    btn01.setOnClickListener(v -> {
                        if (mOnItemOrderEvent != null) mOnItemOrderEvent.onOrderPay(pos);
                    });
                    btn02.setOnClickListener(v -> {
                        if (mOnItemOrderEvent != null) mOnItemOrderEvent.onOrderCancel(pos);
                    });

                }

            } else if (result.getStatus() == 1 || result.getStatus() == 2 || result.getStatus() == 3) {//接单中||待服务||服务中
                btn01.setVisibility(View.GONE);
                btn02.setText("查看详情");
                btn02.setVisibility(View.VISIBLE);
                btn03.setVisibility(View.GONE);
                btn02.setOnClickListener(v -> {
                    if (mOnItemOrderEvent != null) mOnItemOrderEvent.onOrderDetail(pos);
                });
            } else if (result.getStatus() == 4) {//待评价
                btn01.setText("再次购买");
                btn02.setText("追加评价");
                btn03.setText("删除订单");
                btn01.setVisibility(View.VISIBLE);
                btn02.setVisibility(View.VISIBLE);
                btn03.setVisibility(View.VISIBLE);
                btn01.setOnClickListener(v -> {
                    if (mOnItemOrderEvent != null) mOnItemOrderEvent.onOrderAgain(pos);
                });
                btn02.setOnClickListener(v -> {
                    if (mOnItemOrderEvent != null) mOnItemOrderEvent.onOrderEvaluate(pos);
                });
                btn03.setOnClickListener(v -> {
                    if (mOnItemOrderEvent != null) mOnItemOrderEvent.onOrderDelete(pos);
                });
            } else if (result.getStatus() == 5) {//已评价
                btn01.setText("再次购买");
                btn02.setText("删除订单");
                btn01.setVisibility(View.VISIBLE);
                btn02.setVisibility(View.VISIBLE);
                btn03.setVisibility(View.GONE);
                btn01.setOnClickListener(v -> {
                    if (mOnItemOrderEvent != null) mOnItemOrderEvent.onOrderAgain(pos);
                });
                btn02.setOnClickListener(v -> {
                    if (mOnItemOrderEvent != null) mOnItemOrderEvent.onOrderDelete(pos);
                });
            } else if (result.getStatus() == 6) {
                btn01.setText("再次购买");
                btn02.setText("删除订单");
                btn01.setVisibility(View.VISIBLE);
                btn02.setVisibility(View.VISIBLE);
                btn03.setVisibility(View.GONE);
                btn01.setOnClickListener(v -> {
                    if (mOnItemOrderEvent != null) mOnItemOrderEvent.onOrderAgain(pos);
                });
                btn02.setOnClickListener(v -> {
                    if (mOnItemOrderEvent != null) mOnItemOrderEvent.onOrderDelete(pos);
                });
            } else {
                btn01.setVisibility(View.GONE);
                btn02.setVisibility(View.GONE);
                btn03.setVisibility(View.GONE);
            }

        }

    }

    private OnItemOrderEvent mOnItemOrderEvent;

    public void setOnItemOrderEvent(OnItemOrderEvent event) {
        mOnItemOrderEvent = event;
    }

    public interface OnItemOrderEvent {
        void onOrderDetail(int position);

        void onOrderCancel(int position);

        void onOrderPay(int position);

        void onOrderAgain(int position);

        void onOrderEvaluate(int position);

        void onOrderDelete(int position);
    }

}
